package
{
	import flash.display.MovieClip;

	public class HomingLauncherObject
	{
		public var name:String;
		public var basepos:Vector2;
		public var view:Vector2;
		public var pos:Vector2;
		public var mdir:Vector2;
		public var speed:Number;
		public var maxspeed:Number;
		public var startaccel:Number;
		public var curaccel:Number;
		public var accelrate:Number;
		public var turnrate:Number;
		public var isHoming:Boolean;
		public var prefireDelay:Number;
		public var fireDelayTimer:Number;
		public var mc:MovieClip;
		public var rocketmc:MovieClip;

		// public var snd:Sound;
		public function HomingLauncherObject()
		{
			this.name = "homing rocket";
			this.basepos = new Vector2(3, 8);
			this.view = new Vector2(4, 56);
			this.pos = new Vector2(0, 9);
			this.mdir = new Vector2(7, 6);
			this.speed = 0;
			this.maxspeed = Main.getInstance().tiles.xw * 0.285714285714286;
			this.startaccel = 0.1;
			this.curaccel = this.startaccel;
			this.accelrate = 1.1;
			this.turnrate = 0.1;
			this.isHoming = false;
			this.prefireDelay = 10;
			this.fireDelayTimer = 0;
			Main.getInstance().objects.Register(this);
			this.mc = Main.getInstance().gfx.CreateSprite("debugHomingLauncherMC", NinjaGraphicsSystem.LAYER_WALLS);
			this.rocketmc = Main.getInstance().gfx.CreateSprite("debugHomingRocketMC", NinjaGraphicsSystem.LAYER_OBJECTS);
			this.mc.visible = false;
			this.rocketmc.visible = true;
			this.mc.gotoAndStop("rocket_waiting");
			// this.snd = new Sound(this.mc);
		}

		public function Destruct()
		{
			Main.getInstance().gfx.DestroyMC(this.mc);
			this.mc = null;
			Main.getInstance().gfx.DestroyMC(this.rocketmc);
			this.rocketmc = null;
			// this.snd = null;
		}

		public function Init(params):void
		{
			var _local2 = params;
			if (_local2.length == 2)
			{
				this.basepos.x = _local2[0];
				this.basepos.y = _local2[1];
				this.StartIdle();
				this.mc.scaleX = (this.mc.scaleY = Main.getInstance().tiles.xw);
				this.mc.x = this.basepos.x;
				this.mc.y = this.basepos.y;
				this.mc.visible = true;
				this.mc.gotoAndStop("rocket_waiting");
				this.rocketmc.scaleX = (this.rocketmc.scaleY = 100);
				this.rocketmc.x = this.basepos.x;
				this.rocketmc.y = this.basepos.y;
				this.rocketmc.visible = false;
			}
		}

		public function UnInit():void
		{
			Main.getInstance().objects.RemoveFromGrid(this);
			Main.getInstance().objects.EndUpdate(this);
			Main.getInstance().objects.EndThink(this);
		}

		public function DumpInitData():String
		{
			return "" + this.basepos.x + NinjaGame.OBJPARAM_SEPERATION_CHAR + this.basepos.y;
		}

		public function IdleAfterDeath():void
		{
			if (this.isHoming)
			{
				this.StartIdle = this.StartIdle_Death;
			}
			else
			{
				Main.getInstance().objects.EndThink(this);
			}
		}

		public function StartIdle_Death():void
		{
			this.isHoming = false;
		}

		public function Draw():void
		{
			this.rocketmc.x = this.pos.x;
			this.rocketmc.y = this.pos.y;
			var _local2 = Main.getInstance().NormToRot(this.mdir.x, this.mdir.y);
			this.rocketmc.rotation = _local2;
			Main.getInstance().particles.SpawnRocketSmoke(this.pos, _local2);
		}

		public var Update:Function;

		public function StartFiring():void
		{
			Main.getInstance().objects.EndThink(this);
			Main.getInstance().objects.StartUpdate(this);
			this.isHoming = true;
			this.fireDelayTimer = 0;
			this.Update = this.Update_PreFire;
		}

		public function StartIdle():void
		{
			this.isHoming = false;
			Main.getInstance().objects.StartThink(this);
		}

		public function FireMissile():void
		{
			this.curaccel = this.startaccel;
			this.speed = 0;
			this.pos.x = this.basepos.x;
			this.pos.y = this.basepos.y;
			Main.getInstance().objects.AddToGrid(this);
			Main.getInstance().objects.StartDraw(this);
			this.Update = this.Update_Homing;
			var _local3 = PlayerObject.player.pos.x - this.basepos.x;
			var _local2 = PlayerObject.player.pos.y - this.basepos.y;
			var len = Math.sqrt((_local3 * _local3) + (_local2 * _local2));
			if (len == 0)
			{
			}
			else
			{
				_local3 = _local3 / len;
				_local2 = _local2 / len;
				this.mdir.x = _local3;
				this.mdir.y = _local2;
			}
			this.rocketmc.visible = true;
			this.mc.gotoAndPlay("rocket_fire");
		}

		public function ExplodeMissile():void
		{
			// this.snd.stop();
			this.rocketmc.visible = false;
			this.mc.gotoAndPlay("rocket_explode");
			Main.getInstance().particles.SpawnExplosion(this.pos);
			Main.getInstance().objects.EndUpdate(this);
			Main.getInstance().objects.EndDraw(this);
			Main.getInstance().objects.RemoveFromGrid(this);
			this.StartIdle();
		}

		public function Think():void
		{
			if (Main.getInstance().QueryRayObj(this.view, this.basepos, PlayerObject.player.pos, PlayerObject.player))
			{
				this.StartFiring();
			}
		}

		public function TestVsPlayer(guy):void
		{
			var _local3 = guy.pos.x - this.pos.x;
			var _local2 = guy.pos.y - this.pos.y;
			var len = Math.sqrt((_local3 * _local3) + (_local2 * _local2));
			if (len < PlayerObject.player.r)
			{
				Main.getInstance().game.KillPlayer(NinjaGame.KILLTYPE_EXPLOSIVE, _local3, _local2, this.pos.x, this.pos.y, this);
				this.ExplodeMissile();
				return;
			}
		}

		public function Update_PreFire():void
		{
			this.fireDelayTimer++;
			if (this.prefireDelay <= this.fireDelayTimer)
			{
				this.FireMissile();
			}
		}

		public var cell:TileMapCell;

		public function Update_Homing():void
		{
			var _local3 = this.pos;
			if (this.speed < this.maxspeed)
			{
				this.curaccel = this.curaccel * this.accelrate;
				this.speed = this.speed + this.curaccel;
			}
			else
			{
				this.speed = this.maxspeed;
			}
			_local3.x = _local3.x + (this.speed * this.mdir.x);
			_local3.y = _local3.y + (this.speed * this.mdir.y);
			if (TestPoint_Tile.QueryPointvsTileMap(_local3.x, _local3.y))
			{
				this.ExplodeMissile();
				return;
			}
			else
			{
				var _local2 = this.cell;
				if (Main.getInstance().objects.Moved(this))
				{
					var newc = this.cell;
					var e;
					if (newc == _local2.nR)
					{
						e = _local2.eR;
					}
					else if (newc == _local2.nL)
					{
						e = _local2.eL;
					}
					else if (newc == _local2.nU)
					{
						e = _local2.eU;
					}
					else if (newc == _local2.nD)
					{
						e = _local2.eD;
					}
					else
					{
						e = TileMapCell.EID_OFF;
					}
					if (e == TileMapCell.EID_SOLID)
					{
						this.ExplodeMissile();
						return;
					}
				}
			}
			var guy = PlayerObject.player;
			var dx = ((2 * guy.pos.x) - guy.oldpos.x) - (_local3.x + (this.speed * this.mdir.x));
			var dy = ((2 * guy.pos.y) - guy.oldpos.y) - (_local3.y + (this.speed * this.mdir.y));
			var len = Math.sqrt((dx * dx) + (dy * dy));
			dx = dx / len;
			dy = dy / len;
			var dp = ((this.mdir.x * dx) + (this.mdir.y * dy));
			var pp = (((-this.mdir.y) * dx) + (this.mdir.x * dy));
			var trate = this.turnrate;
			if (dp < 0)
			{
			}
			var tempX = (pp * (-this.mdir.y));
			var tempY = (pp * this.mdir.x);
			this.mdir.x = this.mdir.x + (tempX * trate);
			this.mdir.y = this.mdir.y + (tempY * trate);
			var len = Math.sqrt((this.mdir.x * this.mdir.x) + (this.mdir.y * this.mdir.y));
			if (len == 0)
			{
			}
			else
			{
				this.mdir.x = this.mdir.x / len;
				this.mdir.y = this.mdir.y / len;
			}
		}
	}
}